package utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;


public class TagClustering {
	
	private final String loading_file = "tag_clusters.txt";
	
	private HashMap<String,TagCluster> tag_cluster = new HashMap<String,TagCluster>();
	private ArrayList<String> tags = new ArrayList<String>();
	private ArrayList<TagCluster> clusters = new ArrayList<TagCluster>();
	private HashMap<String,Integer> tag_idx = new HashMap<String,Integer>();
	private HashMap<String,Integer> cluster_idx = new HashMap<String,Integer>();
	
	
	private void loadTagsAndClusters(String loading_file) {
		try {
			BufferedReader jin = new BufferedReader(new InputStreamReader(new FileInputStream(loading_file)));
			int i = 0;
			int k = 0;
			while ( jin.ready() ) {
				String line = jin.readLine();
				TagCluster tc = new TagCluster(line);
				for ( String t : tc.getTags() ) {
					tags.add(t);
					tag_idx.put(t,i++);
					tag_cluster.put(t, tc);
				}
				cluster_idx.put(tc.toString(),k++);
				clusters.add(tc);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	public TagClustering() {
		loadTagsAndClusters(loading_file);
	}
	
	public TagClustering(String loading_file) {
		loadTagsAndClusters(loading_file);
	}
	
	public TagCluster getClusterForTag(String tag) {
		return tag_cluster.get(tag);
	}

	public boolean isRelevant(String tag) {
		return tag_cluster.containsKey(tag);
	}
	
	public ArrayList<String> getAllRelevantTags() {
		return tags;
	}
	
	public ArrayList<TagCluster> getAllClusters() {
		return clusters;
	}
	
	public ArrayList<String> getAllClusterNames() {
		ArrayList<String> res = new ArrayList<String>();
		for ( TagCluster tc : getAllClusters() ) {
			res.add(tc.toString());
		}
		return res;
	}
	
	public double getClusterFreq(String cluster_name) {
		return getClusterForTag(cluster_name).getClusterFreq();
	}
	
	public double getTagFreq(String tag) {
		return getClusterForTag(tag).getTagFreq(tag);
	}
	
	public double getTagRelativeFreq(String tag) {
		return getClusterForTag(tag).getTagRealtiveFreq(tag);
	}
	
	public int getTagIdx(String tag) {
		return tag_idx.get(tag);
	}
	
	public int getClusterIdx(String cluster) {
		return cluster_idx.get(cluster);
	}
	
	public int getNumClusters() {
		return clusters.size();
	}
	
	public int getNumTags() {
		return tags.size();
	}

	public TagCluster getCluster(String cluster) {
		return getClusterForTag(cluster);
	}
	
}
